package com.bishe.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.entitis.Company;
import com.api.entitis.JobsInfo;
import com.api.entitis.User;
import com.api.entitis.model.LoginModel;
import com.api.entitis.model.UserModel;
import com.api.entitis.query.CompanyQuery;
import com.api.service.ICompanyService;
import com.api.service.IJobsInfoService;
import com.api.util.CompanyStpUtil;
import com.api.util.JobUtils;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Autowired
    private IJobsInfoService jobsInfoService;

    @Override
    public UserModel login(LoginModel loginModel) {
        String email = loginModel.getUserName();
        String password = loginModel.getPassword();
        Company one = this.getOne(new LambdaQueryWrapper<Company>()
                        .eq(Company::getCompanyEmail, email)
                        .eq(Company::getPassword, password),
                false
        );

        if (one == null){
            return null;
        }
        if (loginModel.getRememberMe() == null){
            loginModel.setRememberMe(false);
        }
        String id = one.getId();
        List<JobsInfo> list = jobsInfoService.list(new LambdaQueryWrapper<JobsInfo>().eq(JobsInfo::getCreateBy, id));
        Optional<String> any =
                list.stream().map(s -> s.getPost()).distinct().findAny();
        // 在登录时写入当前会话的账号id
        CompanyStpUtil.login(one.getId(),loginModel.getRememberMe());
        UserModel userModel = new UserModel();
        any.ifPresent(userModel::setTypeName);
        userModel.setId(one.getId());
        userModel.setName(one.getCompanyName());
        userModel.setType("company");
        userModel.setToken(UserStpUtil.getTokenValue());
        return userModel;
    }

    @Override
    public void registerOrUpdate(Company company) {
        //根据企业名称查询
        Company one = this.getOne(new LambdaQueryWrapper<Company>()
                        .eq(Company::getCompanyName, company.getCompanyName()),
                false
        );

        // 不存在 添加
        if (one == null){
            company.setId(UUID.randomUUID().toString());
            company.setCreateTime(new Date());

            save(company);
            return;
        }

        // 存在 修改
        boolean login = StpUtil.isLogin();
        if (login){
            // 当前登陆人
            String loginId = (String) CompanyStpUtil.getLoginId();
            //当前登陆人和企业创建人一样 修改
            if (loginId.equals(one.getId())){
                company.setCreateTime(one.getCreateTime());
                company.setCompanyName(one.getCompanyName());

                updateById(company);
                return;
            }

            //不是当前登陆人
            throw new EcoBootException("没有权限修改！");
        }
    }

    @Override
    public IPage<Company> toPage(CompanyQuery query) {

        // 条件构造器
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        //企业名不为空 模糊查询
        if (!StrUtil.isEmpty(query.getCompanyName())){
            wrapper.like(Company::getCompanyName,query.getCompanyName());
        }
        // 倒叙排序
        wrapper.orderByAsc(Company::getCreateTime);

        //返回分页数据
        IPage<Company> page = this.page(query.toPage(), wrapper);

        return  page;
    }

    @Override
    public List<JobsInfo> getJobList() {
        String loginId = JobUtils.getLoginId();
        List<JobsInfo> list = jobsInfoService.list(new LambdaQueryWrapper<JobsInfo>()
                .eq(JobsInfo::getCreateBy, loginId));

        return list;
    }
}
